package com.cc2019.buke.user.tools;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


import androidx.annotation.Nullable;

public class DataBaseHelper extends SQLiteOpenHelper {

    private static DataBaseHelper instance;
    private static final String TAG="DatabaseHelper";
//    public static final String USERINFO=Constants.TABLE_NAME;
    public DataBaseHelper(@Nullable Context context, String name, SQLiteDatabase.CursorFactory factory,int version) {
        super(context, name, factory, version);
    }

    //创建数据库(这只会在程序第一次运行时生效，db文件会被储存在移动设备中，下次运行不会重复创建)
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG,"创建数据库...");
        //创建数据库中的字段，这里只创建了一个name字段和一个password字段
        String sql1 ="create table "+ Constants.TABLE_NAME+"(id text primary key,name varchar(20),password varchar(20),email varchar(20))";
        db.execSQL(sql1);
    }

    //增加
    public void insert(SQLiteDatabase sqLiteDatabase,String id, String name,String password,String email){
        //声明键值对values
        ContentValues values=new ContentValues();
        //分别插入名为name和password的值到键值对values的"name"和"password"字段中
        values.put("id",id);
        values.put("name",name);
        values.put("password",password);
        values.put("email",email);
        //将键值对values插入到数据库中
        sqLiteDatabase.insert(Constants.TABLE_NAME,null,values);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public static DataBaseHelper getInstance(Context context){
        if(instance==null){
            instance=new DataBaseHelper(context,"user.db",null,2);
        }
        return instance;
    }
}



